package com.abc.cloud.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Query;

import com.abc.cloud.dao.SysConfigDao;
import com.abc.cloud.entity.SysConfigEntity;
import com.abc.cloud.service.SysConfigService;

import static com.abc.cloud.common.constants.Constants.TOTAL_SPACE;


@Service("sysConfigService")
public class SysConfigServiceImpl extends ServiceImpl<SysConfigDao, SysConfigEntity> implements SysConfigService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        IPage<SysConfigEntity> page = this.page(
                new Query<SysConfigEntity>().getPage(params),
                new QueryWrapper<SysConfigEntity>()
        );

        return new PageUtil(page);
    }

    @Override
    public List<SysConfigEntity> queryAll() {
        return this.baseMapper.selectList(new QueryWrapper<SysConfigEntity>().eq("status", 1));
    }

    @Override
    public void updateSysConfigList(List<SysConfigEntity> sysConfigList) {
        this.baseMapper.updateSysConfigList(sysConfigList);

        // 修改redis缓存的系统空间
        SysConfigEntity sysConfigEntity = sysConfigList.get(0);
        redisTemplate.opsForValue().set(TOTAL_SPACE, sysConfigEntity.getSysValue());
    }

    @Override
    public SysConfigEntity queryKey(String key) {
        return this.baseMapper.selectOne(new QueryWrapper<SysConfigEntity>().eq("sys_key", key));
    }
}